package canvas 
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.SWFLoader;
	import com.util.Bezier;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import framework.ILvCanvas;
	import framework.LayerManager;
	import framework.Timepiece;
	import temp.GetProp;
	
	/**
	 * 等级一的管理
	 * @author Henrylee{henrylee_sz@qq.com}
	 */
	public class LvCanvas1 implements ILvCanvas
	{
		
		private var rawList: Array = ["raw/Archer_Rabbit_1.swf", "raw/Monster_1.swf", "raw/Tower_1.swf"];
		
		private var queue: LoaderMax;
		private var monster:MovieClip;
		private var weapon:MovieClip;
		private var tower:MovieClip;
		private var rabbit:MovieClip;
		private var steps:int;
		private var startPoint:Point = new Point(100, 100);
		private var controlPoint:Point = new Point(250, -100);
		private var endPoint:Point = new Point(300, 300);
		private var crtStep:int;
		
		public function LvCanvas1() 
		{
			
		}
		
		
		private function initialize():void
		{
			queue = new LoaderMax( { name:"mainQueue", onComplete:completeHandler } );
			var len: int = rawList.length;
			while (len--)
				queue.append(new SWFLoader(rawList[len]));
				
			queue.load();
		}
		
		private function completeHandler(event:LoaderEvent):void
		{
			addAvatar();
			
			steps = Bezier.init(startPoint, controlPoint, endPoint, 10);
			crtStep = 0;
			Timepiece.instance.addFun(loopEfHandler);
		}
		
		private function loopEfHandler() : void
        {
            var _loc_2:Array = Bezier.getAnchorPoint(crtStep);
            weapon.x = _loc_2[0];
            weapon.y = _loc_2[1];
            weapon.rotation = _loc_2[2];
			crtStep += 1;
            if (crtStep > steps)
            {
                crtStep = 0;
                rabbit.gotoAndPlay(2);
            }
        }
		
		
		
		private function addAvatar():void
		{
			var CLASS: Class;
			var cd: SWFLoader = LoaderMax.getLoader(rawList[0]) as SWFLoader;
			CLASS = cd.getClass("Archer_Rabbit_1") as Class;
			rabbit = new CLASS();
			CLASS = cd.getClass("Archer_Rabbit_Weapon") as Class;
			weapon = new CLASS();
			
			cd = LoaderMax.getLoader(rawList[1]) as SWFLoader;
			CLASS = cd.getClass("Monster_1") as Class;
			monster = new CLASS();
			
			cd = LoaderMax.getLoader(rawList[2]) as SWFLoader;
			CLASS = cd.getClass("Tower_1") as Class;
			tower = new CLASS();
			
			
			rabbit.x = 100;
			rabbit.y = 100;
			tower.x = 100;
			tower.y = 100;
			monster.x = 300;
			monster.y = 300;
			LayerManager.instance.addToLayer(LayerManager.GAMELAYER, tower);
			LayerManager.instance.addToLayer(LayerManager.GAMELAYER, rabbit);
			LayerManager.instance.addToLayer(LayerManager.GAMELAYER, monster);
			LayerManager.instance.addToLayer(LayerManager.GAMELAYER, weapon);
		}
		
		
		/* INTERFACE framework.ILvCanvas */
		
		public function active():void 
		{
			initialize();
		}
		
		public function disable():void 
		{
			Timepiece.instance.removeFun(loopEfHandler, Timepiece.FRAME);
			rabbit && rabbit.parent && rabbit.parent.removeChild(rabbit);
			tower && tower.parent && tower.parent.removeChild(tower);
			weapon && weapon.parent && weapon.parent.removeChild(weapon);
			monster && monster.parent && monster.parent.removeChild(monster);
		}
		
	}

}